package com.lw.spring.javademo.cookie;


import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/rececookie")
public class ReceiveCookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求中的所有 Cookie
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            // 遍历所有的 Cookie 并查找 "userToken"
            for (Cookie cookie : cookies) {
                if ("userToken".equals(cookie.getName())) {
                    // 如果找到 "userToken" Cookie，输出它的值
                    System.out.println(cookie.getName());
                    response.getWriter().write("Received Cookie 'userToken': " + cookie.getValue());
                    return;
                }
            }
        }
        // 如果没有找到 "userToken" Cookie
        response.getWriter().write("No 'userToken' Cookie found!");
    }
}

